package com.example.spinning.controller;


import com.example.spinning.bean.Category;
import com.example.spinning.bean.Organ;
import com.example.spinning.bean.RespBean;
import com.example.spinning.bean.endProductStock;
import com.example.spinning.service.CategoryService;
import com.example.spinning.service.OrganService;
import com.example.spinning.utils.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机构管理
 */
@RestController
@RequestMapping("/organ")
public class OrganController {
    @Autowired
    OrganService organService;

//    @RequestMapping(value = "/getSpecificValue", method = RequestMethod.GET)
//    public List<Organ> getSpecificOrgans(Organ organ) {
//        return organService.getSpecificOrgans(organ);
//    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Organ> getAllOrgans() {
        return organService.getAllOrgans();
    }

//    @RequestMapping(value = "/getSpecificValue", method = RequestMethod.GET)
//    public List<Organ> getSpecificOrgans(Organ organ) {
//        return organService.getSpecificOrgans(organ);
//    }

    //按条件查找
    @RequestMapping(path = "/getSpecificValue/",method = RequestMethod.GET)
    public Map<String,Object> getSpecificOrgans(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            Organ organ
    ){
        int totalCount = organService.getOrganCountByCondition(organ);
        List<Organ> organ_list = organService.getSpecificOrgans(page,count,organ);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("organ",organ_list);
        return map;
    }


    @Log(module = "机构管理",description = "删除机构")
    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    public RespBean deleteById(@PathVariable String ids) {
        boolean result = organService.deleteOrganByIds(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    @Log(module = "机构管理",description = "添加机构")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public RespBean addNewOrgan(Organ organ) {
        if ("".equals(organ.getOrgan_id()) || organ.getOrgan_id() == null) {
            return new RespBean("error", "请输入机构编号!");
        }
        if ("".equals(organ.getOrgan_name()) || organ.getOrgan_name() == null) {
            return new RespBean("error", "请输入机构名称!");
        }
        if ("".equals(organ.getConnection()) || organ.getConnection() == null) {
            return new RespBean("error", "请输入机构负责人!");
        }
        int result = organService.addOrgan(organ);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    @Log(module = "机构管理",description = "修改机构")
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public RespBean updateOrgan(Organ organ) {
        int i = organService.updateOrganById(organ);
        if (i == 1) {
            return new RespBean("success", "修改成功!");
        }
        return new RespBean("error", "修改失败!");
    }
}
